/**
 * 带单位的字节数据
 * @param {Number} byteSize 字节
 * @param {Number} fixed 小数保留位
 * @returns {String} 0.00MB
 */
export const toByteSize = (byteSize = 0, fixed = 2) => {
  return (
    abbrNum({ value: byteSize, base: 1024, fixed, unitCapital: true }) + "B"
  );
};

/**
 * 简化数字
 */
export function abbrNum({
  value = 0,
  fixed = 2,
  base = 1000,
  unitCapital = false,
}) {
  const K = base ** 1;
  const M = base ** 2;
  const G = base ** 3;
  const T = base ** 4;

  let size = value;
  let unit = "T";
  if (size >= T) {
    size = size / T;
    unit = "T";
  } else if (size >= G) {
    size = size / G;
    unit = "G";
  } else if (size >= M) {
    size = size / M;
    unit = "M";
  } else if (size > K) {
    size = size / K;
    unit = "K";
  } else {
    unit = "";
  }
  unit = unitCapital ? unit.toUpperCase() : unit.toLowerCase();
  return (size % 1 === 0 ? size : size.toFixed(fixed)) + unit;
}
